<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 面包屑导航区 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>资源管理</el-breadcrumb-item>
        <el-breadcrumb-item>计协资源</el-breadcrumb-item>
      </el-breadcrumb>

      <!-- 搜索区 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            clearable
            v-model="queryInfo.query">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searchResources">
            </el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-dropdown>
            <span class="el-dropdown-link">
              显示资源<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="displayAll">显示全部资源</el-dropdown-item>
              <el-dropdown-item @click.native="displayAudited">显示已审核资源</el-dropdown-item>
              <el-dropdown-item @click.native="displayUnaudited">显示未审核资源</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="addClick">添加资源</el-button>
        </el-col>
        <el-col :span="2">
          <el-button type="danger" @click="batchDelete">批量删除</el-button>
        </el-col>
        <el-col :span="2">
          <el-button type="warning" @click="batchRevers">反选</el-button>
        </el-col>
      </el-row>

      <!-- 资源表格区 -->
      <el-table
        :header-cell-style="{textAlign: 'center'}"
        :cell-style="{ textAlign: 'center' }"
        @selection-change="handleSelectionChange"
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          prop="id"
          label="序号"
          width="80">
        </el-table-column>
        <el-table-column
          prop="name"
          label="资源名称"
          width="150">
        </el-table-column>
        <el-table-column
          prop="time"
          label="上传时间"
          width="130">
        </el-table-column>
        <el-table-column
          prop="url"
          label="资源链接"
          width="180">
        </el-table-column>
        <el-table-column
          prop="urlsort"
          label="链接分类"
          width="150">
        </el-table-column>
        <el-table-column
          prop="inforomationsort"
          label="资源分类"
          width="150">
        </el-table-column>
        <el-table-column
          prop="extract_code"
          label="提取码"
          width="80">
        </el-table-column>
        <el-table-column
          prop="display"
          label="审核"
          width="70">
          <template slot-scope='scope'>
            <el-button 
              v-if="scope.row.display==1" 
              type="success" 
              size="mini"
              icon="el-icon-check"
              @click="batchRever(scope.row.id)"></el-button>
            <el-button
              v-else
              type="danger"
              size="mini" 
              icon="el-icon-close"
              @click="batchRever(scope.row.id)"></el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button 
              type="primary" 
              icon="el-icon-edit" 
              size="mini"
              @click="editClick(scope.row)"></el-button>
            <el-button 
              type="danger" 
              icon="el-icon-delete" 
              size="mini" 
              @click="delResources(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background>
      </el-pagination>
    </el-card>

    <!-- 操作资源对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="opeDialogVisible"
      width="50%">
      <el-form
        :model="opeForm"
        :rules="opeFormRules"
        ref="opeFormRef"
        label-width="100px">
        <el-form-item label="序号" prop="id">
          <el-input v-model="opeForm.id" :disabled='true'></el-input>
        </el-form-item>
        <el-form-item label="审核" prop="display">
          <el-input v-model="opeForm.display" :disabled='true'></el-input>
        </el-form-item>
        <el-form-item label="上传时间" prop="time">
           <el-date-picker
            v-model="opeForm.time"
            align="right"
            type="date"
            placeholder="选择日期"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="资源名称" prop="name">
          <el-input v-model="opeForm.name"></el-input>
        </el-form-item>
        <el-form-item label="资源链接" prop="url">
          <el-input v-model="opeForm.url"></el-input>
        </el-form-item>
        <el-form-item label="链接分类" prop="urlsort">
          <el-input v-model="opeForm.urlsort"></el-input>
        </el-form-item>
        <el-form-item label="资源分类" prop="inforomationsort">
          <el-input v-model="opeForm.inforomationsort"></el-input>
        </el-form-item>
        <el-form-item label="提取码" prop="extract_code">
          <el-input v-model="opeForm.extract_code"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="opeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="operation">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.findResourcesByDisplay()
  },
  data() {
    return {
      // 获取所有资源的查询参数
      queryInfo: {
        pageNum: 1,
        pageSize: 10,
        query: "",
        display: 0
      },
      total: 0,
      tableData: [],
      // 操作资源需要的数据
      title: '',
      opeDialogVisible: false,
      opeForm: {
        display: 0,
        extract_code: "",
        id: 0,
        inforomationsort: 0,
        name: "",
        time: '',
        url: "",
        urlsort: 0
      },
      opeFormRules: {
        name: [
          { required: true, message: '资源名称是必填项', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符之间', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '资源链接是必填项', trigger: 'blur' },
        ],
        urlsort: [
          { required: true, message: '链接分类是必填项', trigger: 'blur' },
        ],
        inforomationsort: [
          { required: true, message: '资源分类是必填项', trigger: 'blur' },
        ]
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      batchList: [],
      flag: 'unaudited'
    }
  },
  methods: {
    // 发送请求前，初始化请求参数，避免上一次发送的请求参数影响到下一次
    init() {
      this.queryInfo.pageNum = 1
      this.queryInfo.pageSize = 10
      this.total = 0
      this.tableData = []
    },
    displayAll() {
      this.flag = 'all'
      this.init()
      this.findResourcesByDisplay()
    },
    displayAudited() {
      this.flag = 'audited'
      this.init()
      this.findResourcesByDisplay()
    },
    displayUnaudited() {
      this.flag = 'unaudited'
      this.init()
      this.findResourcesByDisplay()
    },
    // 根据条件显示资源
    async findResourcesByDisplay() {
      if (this.flag == 'all') {
        this.queryInfo.display = -1
      } else if (this.flag == 'audited') {
        this.queryInfo.display = 1
      } else {
        this.queryInfo.display = 0
      }
      const { data: res } = await this.$http.post('allJxCloud', this.queryInfo)
      if (res.code != 'success') return this.$message.error('获取计协资源失败')
      this.tableData = res.data
      this.total = res.numbers
    },
    // 添加资源和修改资源相关
    addClick() {
      this.title = '添加资源'
      this.opeForm.id = 0
      this.opeForm.display = 0
      this.opeForm.time = ''
      this.opeForm.name = ''
      this.opeForm.url = ''
      this.opeForm.urlsort = 0
      this.opeForm.inforomationsort = ''
      this.opeForm.extract_code = ''
      this.opeDialogVisible = true
    },
    editClick(resources) {
      this.title = '修改资源信息'
      this.opeForm.id = resources.id
      this.opeForm.display = resources.display
      this.opeForm.time = resources.time
      this.opeForm.name = resources.name
      this.opeForm.url = resources.url
      this.opeForm.urlsort = resources.urlsort
      this.opeForm.inforomationsort = resources.inforomationsort
      this.opeForm.extract_code = resources.extract_code
      this.opeDialogVisible = true
    },
    async operation() {
      if (this.title == '添加资源') {
        const { data: res } = await this.$http.post('insertJxCloud', this.opeForm)
        if (res != 'success') return this.$message.error('添加资源失败')
        this.$message.success('添加资源成功')
        this.opeDialogVisible = false
        this.getAllResources()
      } else {
        const { data: res } = await this.$http.post('updateJxCloud', this.opeForm)
        if (res != 'success') return this.$message.error('修改资源信息失败')
        this.$message.success('修改资源信息成功')
        this.opeDialogVisible = false
        this.getAllResources()
      }
    },
    // 删除资源
    async delResources(id) {
      const confirmResult = await this.$confirm('此操作将永久删除该资源, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.post('batchDeleteJxCloud', { ids: id })
      if (res !== 'success') return this.$message.error('删除资源失败')
      this.$message.success('删除资源成功')
      this.getAllResources()
    },
    // 查询资源
    async searchResources() {
      const { data: res } = await this.$http.post('allJxCloud', this.queryInfo)
      if (res.code !== 'success') return this.$message.error('查询资源失败')
      this.tableData = res.data
      this.$message.success('查询资源成功')
    },
    // 批量选择
    handleSelectionChange(val) {
      this.batchList = val
    },
    // 批量删除
    async batchDelete() {
      var list = []
      this.batchList.forEach(item => list.push(item.id))
      // console.log(list.join(','))
      const confirmResult = await this.$confirm('此操作将永久删除这些资源, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.post('batchDeleteJxCloud', { ids: list.join(',') })
      if (res !== 'success') return this.$message.error('批量删除资源失败')
      this.$message.success('批量删除资源成功')
      this.findResourcesByDisplay()
    },
    // 单个反转
    async batchRever(id) {
      const { data: res } = await this.$http.post('batchReversDisplayJxcloud', { ids: id })
      if (res !== 'success') return this.$message.error('反转资源状态失败')
      this.$message.success('反转资源状态成功')
      this.findResourcesByDisplay()
    },
    // 批量反转
    async batchRevers() {
      var list = []
      this.batchList.forEach(item => list.push(item.id))
      const { data: res } = await this.$http.post('batchReversDisplayJxcloud', { ids: list.join(',') })
      if (res !== 'success') return this.$message.error('批量反转资源状态失败')
      this.$message.success('批量反转资源状态成功')
      this.findResourcesByDisplay()
    },
    // 每页显示条数发生改变时
    handleSizeChange(size) {
      this.queryInfo.pageSize = size
      this.findResourcesByDisplay()
    },
    // 当前页码发生改变时
    handleCurrentChange(currentPage) {
      this.queryInfo.pageNum = currentPage
      this.findResourcesByDisplay()
    },
    
  }
}
</script>

<style scoped>
.el-icon-arrow-down {
  margin-top: 12px;
}
</style>
